package com.htht.smartschool.service.tschool;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.htht.smartschool.common.service.BaseService;
import com.htht.smartschool.dao.user.UserDao;
import com.htht.smartschool.model.tschool.TSchool;

public class SchoolService extends BaseService {
	UserDao userDao = new UserDao();

	/***
	 * 按分页获取学校信息
	 * @param currentpage
	 * @param pagesize
	 * @return result
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map getSchoolByPage(TSchool tschool) {
		 
		Map map = new HashMap();
		String sql = "select * from t_school";
		String sql2 = "select count(*) from t_school";
		if(tschool.getSchoolcode() != null && tschool.getSchoolcode() != ""){
			sql += " where schoolcode = :schoolcode";
			sql2 += " where schoolcode = :schoolcode";
		}
		List<Object[]> list = getDao().queryBySQL(sql, tschool);
		List<TSchool> result = new ArrayList<TSchool>();
		int count = getDao().getCountBySqlPage(sql2,tschool);

		for (int i = 0; i < list.size(); i++) {
			Object[] obj = list.get(i);

			TSchool ts = new TSchool();
			ts.setSchoolcode(String.valueOf(obj[1]));
			ts.setName(String.valueOf(obj[2]));
			ts.setAdress(String.valueOf(obj[3]));
			ts.setCenter(String.valueOf(obj[4]));
			ts.setRadius(String.valueOf(obj[5]));
			ts.setRemark(String.valueOf(obj[6]));
			result.add(ts);
		}
		map.put("total", count);
		map.put("data", result);
		return map;
	}

	/**
	 * 新增学校
	 * @param tschool
	 * @return
	 */
	public TSchool addSchoolInfo(TSchool tschool) {
		TSchool school = getSchoolByCode(tschool.getSchoolcode());
		if (school != null) {
			return null;
		} else {
			tschool = (TSchool) getDao().create(tschool);
		}
		return tschool;
	}

	/**
	 * 更新学校信息
	 * @param tschool
	 * @return
	 */
	public boolean updateTSchoolInfo(TSchool tschool) {
		boolean flag = false;
		TSchool school = getSchoolByCode(tschool.getSchoolcode());
		if (school != null) {
			for (Field f : tschool.getClass().getDeclaredFields()) {
				f.setAccessible(true);
				try {
					if (f.get(tschool) != null && !f.getName().equalsIgnoreCase("serialVersionUID")) {
						f.set(school, f.get(tschool));
					}
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
			tschool = (TSchool) getDao().update(school);
			flag = true;
		}
		return flag;
	}

	/**
	 * 删除学校
	 * @param tSchool
	 * @return
	 */
	public boolean deleteTSchoolInfo(TSchool tSchool) {
		boolean flag = false;
		if (tSchool != null && !tSchool.getSchoolcode().equals("")) {
			String[] table = new String[] { "t_attendance_regular", "t_big_exam",
					"t_big_exam_classes", "t_class", "t_coursesubject", "t_department",
					"t_exam", "t_message", "t_result", "t_role",
					"t_school","t_student","t_teacher","t_user"};
			String sql = " delete from ";
			for(int i = 0;i<table.length;i++){
				String sql2 = sql + table[i];
				if(table[i] == "t_attendance_regular"){
					sql2 = sql2 + "  where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_big_exam"){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_big_exam_classes"){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_class"){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_coursesubject"){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_department"){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_exam"){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_message"){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_result"){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_role"){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_school"){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_student" ){
					sql2 = sql2 + " where schoolid = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_teacher" ){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}else if(table[i] == "t_user"){
					sql2 = sql2 + " where schoolcode = '"+tSchool.getSchoolcode()+"'";
				}
				table[i] = sql2;
				sql2 = "";
			}
			if (getDao().deleteSQL(table) > 0) {
				flag = true;
			}
//			String sql = "delete from t_school where schoolcode = :schoolcode";
//			int state = getDao().executeSQL(sql,tSchool);
//			if (state >= 0) {
//				flag = true;
//				//当删除学校信息的时候将这个学校绑定的学生的schoolid设置成null
//				String ssql = " update t_student set schoolid = null where schoolid = :schoolcode";
//				int a = getDao().executeSQL(ssql,tSchool);
//				if( a >= 0){
//					flag = true;
//				}
				
//			}
		}
		return flag;
	}

	/**
	 * 验证是否已有指定Code学校
	 * @param schoolcode
	 * @return
	 */
	public TSchool getSchoolByCode(String schoolcode) {
		TSchool ts = null;
		try {
			 String hql = "From TSchool where schoolcode = " + schoolcode;
			 ts = (TSchool)getDao().queryFirstByHQL(hql);
			//下边的方法也可以。
			/*			
			String sql = "select * from t_school where schoolcode = '"+ schoolcode + "'";
			List<Object[]> list = getDao().getEntityBySql(sql);
			if (list.size() > 0) {
				Object[] obj = list.get(0);
				ts = new TSchool();
				ts.setSchoolcode(String.valueOf(obj[1]));
				ts.setName(String.valueOf(obj[2]));
				ts.setAdress(String.valueOf(obj[3]));
				Blob blob = (Blob) obj[4];
				String fenceStr = "";
				try {
					fenceStr = new String(blob.getBytes((long) 1,
							(int) blob.length()));
				} catch (SQLException e) {
					e.printStackTrace();
				}
				List<String> pointList = new ArrayList<String>();
				String[] lst = fenceStr.split("&");
				for (int j = 0; j < lst.length; j++) {
					pointList.add(lst[j]);
				}
				ts.setSchoolfence(pointList);
			}
			*/
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ts;
	}
}
